import { dirname, join } from "path";
import { fileURLToPath } from "url";
import fs from "fs";
import remark from "remark-parse";
import { unified } from "unified";

const __dirname = dirname(fileURLToPath(import.meta.url));

const markdown = await fs.promises.readFile(
  join(__dirname, "../logs/cargo.md"),
  "utf-8"
);
const ast = unified().use(remark).parse(markdown);

const rows = ast?.children
  ?.filter((node: any) => node.type === "code" && "json" === node.lang)
  ?.map((node: any) => JSON.parse(node.value ?? "[]"))
  ?.filter((row: any) => row)
  ?.reduce((prev: any, curr: any) => prev.concat(curr), []);

console.log(rows);
